package fileReader.demo.reader;

import java.awt.geom.Point2D;
import java.io.DataInputStream;
import java.io.IOException;

public class LayerReader extends MapLayer{

	public LayerReader() {
		super();
	}

	protected void skipLayerBytes(DataInputStream dataIn) throws IOException {
		dataIn.skipBytes(17);	// SPATQUALID
		dataIn.skipBytes(8);	// DATECHANGE
		dataIn.skipBytes(17);	// ORGDDOCOID
		dataIn.skipBytes(12);	// SCAMIN
		dataIn.skipBytes(254);	// INFORMATIO
		dataIn.skipBytes(50);	// NTMENTRY	
		dataIn.skipBytes(12);	// SCAMAX
		dataIn.skipBytes(17);	// LSTDDOCOID
		dataIn.skipBytes(17);	// OBJECTID
		dataIn.skipBytes(12);	// RECOPER
		dataIn.skipBytes(17);	// JOBID
		dataIn.skipBytes(12);	// VALIDATOR
		dataIn.skipBytes(254);	// INT_INFORM
	}

	/**
	 * Swabs the byteorder to little-endian to big-endian
	 * or to big-endian to little-endian.
	 * @param value byte value which to swap
	 * @return swapped
	 */
	public static final int swabInt(int value) {
	    return  (value >>> 24) | (value << 24) | 
	      ((value << 8) & 0x00FF0000) | ((value >> 8) & 0x0000FF00);
	 }
	
	/**
	 * This reads a big endian doubles and swaps
	 * the bit order to big endian.
	 * @param value double
	 * @return swapped double value
	 */
	public double readLittleDouble(double value) {
		long test = swabDouble(value);
		return Double.longBitsToDouble(test);
	
	}

	/**
	 * This reads a big endian short and swaps
	 * the bit order to big endian.
	 * @param value short
	 * @return swapped short value
	 */
	public short readLittleShort(short value) {
		return swabShort(value);
	}

	/**
	 * Swabs the byteorder to little-endian to big-endian
	 * or to big-endian to little-endian.
	 * @param value byte value which to swap
	 * @return swapped bits
	 */
	private short swabShort(short value) {
		  int bit1 = value & 0xff;
		  int bit2 = (value >> 8) & 0xff;
	
		  return (short)(bit1 << 8 | bit2 << 0);
	}

	/**
	 * Swabs the byteorder to little-endian to big-endian
	 * or to big-endian to little-endian.
	 * @param value byte value which to swap
	 * @return swapped bits
	 */
	private long swabDouble(double value) {
		long longValue = Double.doubleToRawLongBits (value);
	    
		long bit1 = (longValue >>  0) & 0xff;
	    long bit2 = (longValue >>  8) & 0xff;
	    long bit3 = (longValue >> 16) & 0xff;
	    long bit4 = (longValue >> 24) & 0xff;
	    long bit5 = (longValue >> 32) & 0xff;
	    long bit6 = (longValue >> 40) & 0xff;
	    long bit7 = (longValue >> 48) & 0xff;
	    long bit8 = (longValue >> 56) & 0xff;
	
	    return bit1 << 56 | bit2 << 48 | bit3 << 40 | bit4 << 32 |
	           bit5 << 24 | bit6 << 16 | bit7 <<  8 | bit8 <<  0;
	}

	/**
	 * This reads a big endian integers and swaps
	 * the bit order to big endian.
	 * @param value integer
	 * @return swapped integer value
	 */
	public int readLittleInteger(int value) {
		return swabInt(value);
	}

	protected void readDataError() {
		Simulator.errorMsg("Error while opening file.");
	}

	protected void recordDeleted() {
		System.out.println("Record destroyed");
	}

	protected Box makeBoundingBox(DataInputStream dataIn) {
		try{
			double xMin = readLittleDouble(dataIn.readDouble());	//boundingbox
			double yMin = readLittleDouble(dataIn.readDouble());	//boundingbox
			double xMax = readLittleDouble(dataIn.readDouble());	//boundingbox
			double yMax = readLittleDouble(dataIn.readDouble());	//boundingbox
			Box bb = new Box(xMin, yMin, xMax, yMax);
			return bb;
		} 
		catch (IOException e) 
		{
			readDataError();
			e.printStackTrace();
		}
		return null;
	}

	protected Point2D makeVertex(DataInputStream dataIn) {
		Point2D vertex=null;
		try{
			double x = readLittleDouble(dataIn.readDouble());
			double y = readLittleDouble(dataIn.readDouble());
			vertex = new Point2D.Double(x,y);
			
		}catch (IOException e) {
		readDataError();
		e.printStackTrace();
		}	
		return vertex;
	}

}